|
|
|
Point 3 of the ADT
definition says that for each access to the data structure there must be an
operation defined. The above access examples seem to contradict this
requirement. Is this really true?
|
|
Let's look again at the two
possibilities for representing imaginary numbers. Let's stick to the real
part. In the first version, x equals c[0]. In the second
version, x equals c.r. In both cases x equals
``something''. It is this ``something'' which differs from the actual data
structure used. But in both cases the performed operation ``equal'' has the
same meaning to declare x to be equal to the real part of the complex
number c: both cases archieve the same semantics.
|
|
If you think of more complex
operations the impact of decoupling data structures from operations becomes
even more clear. For example the addition of two complex numbers requires you
to perform an addition for each part. Consequently, you must access the value
of each part which is different for each version. By providing an operation
``add'' you can encapsulate these details from its actual use. In an
application context you simply ``add two complex numbers'' regardless of how
this functionality is actually archieved.
|
|
Once you have created an ADT
for complex numbers, say Complex, you can use it in the same way like
well-known data types such as integers.
|
|
|